﻿// -----------------------------------------------------------------------
// <copyright file="PhieuThamDoChucNangDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class PhieuThamDoChucNangDataAccess
    {
        public bool InsertPhieuThamDoChucNang(PhieuThamDoChucNangEntity obj)
        {
            try
            {
                string sql = "spPhieuThamDoChucNang_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaThamDoChucNang", DbType.String, obj.MaThamDoChucNang);
                db.AddInParameter(cmd, "Stt", DbType.Int32, obj.Stt);
                db.AddInParameter(cmd, "SoVaoVien", DbType.String, obj.SoVaoVien);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "YeuCau", DbType.String, obj.YeuCau);
                db.AddInParameter(cmd, "GhiChu", DbType.String, obj.GhiChu);
                db.AddInParameter(cmd, "KetQua", DbType.String, obj.KetQua);
                db.AddInParameter(cmd, "KetLuan", DbType.String, obj.KetLuan);
                db.AddInParameter(cmd, "MaBacSyKham", DbType.String, obj.MaBacSyKham);
                db.AddInParameter(cmd, "MaBacSyCls", DbType.String, obj.MaBacSyCls);
                db.AddInParameter(cmd, "Anh1", DbType.String, obj.Anh1);
                db.AddInParameter(cmd, "Anh2", DbType.String, obj.Anh2);
                db.AddInParameter(cmd, "Anh3", DbType.String, obj.Anh3);
                db.AddInParameter(cmd, "Anh4", DbType.String, obj.Anh4);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.Int32, obj.TrangThaiPhieu);
                db.AddInParameter(cmd, "MaKhoaCanLamSang", DbType.String, obj.MaKhoaCanLamSang);
                db.AddInParameter(cmd, "MaPhongCanLamSang", DbType.String, obj.MaPhongCanLamSang);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "MaKhoaKham", DbType.String, obj.MaKhoaKham);
                db.AddInParameter(cmd, "MaPhongKham", DbType.String, obj.MaPhongKham);
                db.AddInParameter(cmd, "TrangThaiCho", DbType.Boolean, obj.TrangThaiCho);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertPhieuThamDoChucNang", ex.Message); throw ex; }
        }

        public bool EditPhieuThamDoChucNang(PhieuThamDoChucNangEntity obj)
        {
            try
            {
                string sql = "spPhieuThamDoChucNang_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaThamDoChucNang", DbType.String, obj.MaThamDoChucNang);
                db.AddInParameter(cmd, "Stt", DbType.Int32, obj.Stt);
                db.AddInParameter(cmd, "SoVaoVien", DbType.String, obj.SoVaoVien);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "YeuCau", DbType.String, obj.YeuCau);
                db.AddInParameter(cmd, "GhiChu", DbType.String, obj.GhiChu);
                db.AddInParameter(cmd, "KetQua", DbType.String, obj.KetQua);
                db.AddInParameter(cmd, "KetLuan", DbType.String, obj.KetLuan);
                db.AddInParameter(cmd, "MaBacSyKham", DbType.String, obj.MaBacSyKham);
                db.AddInParameter(cmd, "MaBacSyCls", DbType.String, obj.MaBacSyCls);
                db.AddInParameter(cmd, "Anh1", DbType.String, obj.Anh1);
                db.AddInParameter(cmd, "Anh2", DbType.String, obj.Anh2);
                db.AddInParameter(cmd, "Anh3", DbType.String, obj.Anh3);
                db.AddInParameter(cmd, "Anh4", DbType.String, obj.Anh4);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.Int32, obj.TrangThaiPhieu);
                db.AddInParameter(cmd, "MaKhoaCanLamSang", DbType.String, obj.MaKhoaCanLamSang);
                db.AddInParameter(cmd, "MaPhongCanLamSang", DbType.String, obj.MaPhongCanLamSang);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "MaKhoaKham", DbType.String, obj.MaKhoaKham);
                db.AddInParameter(cmd, "MaPhongKham", DbType.String, obj.MaPhongKham);
                db.AddInParameter(cmd, "TrangThaiCho", DbType.Boolean, obj.TrangThaiCho);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdatePhieuThamDoChucNang", ex.Message); throw ex; }
        }

        public bool DeletePhieuThamDoChucNang(string maThamDoChucNang)
        {
            try
            {
                string sql = "spPhieuThamDoChucNang_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaThamDoChucNang", DbType.String, maThamDoChucNang);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuThamDoChucNang", ex.Message); throw ex; }
        }

        public PhieuThamDoChucNangEntity GetPhieuThamDoChucNang(string maThamDoChucNang)
        {
            try
            {
                PhieuThamDoChucNangEntity obj = null;
                string sql = "spPhieuThamDoChucNang_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaThamDoChucNang", DbType.String, maThamDoChucNang);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuThamDoChucNangEntity();
                        obj.MaThamDoChucNang = reader["MaThamDoChucNang"].ToString();
                        obj.Stt = String.IsNullOrEmpty(reader["Stt"].ToString()) == true ? 0 : (Int32)reader["Stt"];
                        obj.SoVaoVien = reader["SoVaoVien"].ToString();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.YeuCau = reader["YeuCau"].ToString();
                        obj.GhiChu = reader["GhiChu"].ToString();
                        obj.KetQua = reader["KetQua"].ToString();
                        obj.KetLuan = reader["KetLuan"].ToString();
                        obj.MaBacSyKham = reader["MaBacSyKham"].ToString();
                        obj.MaBacSyCls = reader["MaBacSyCls"].ToString();
                        obj.Anh1 = reader["Anh1"].ToString();
                        obj.Anh2 = reader["Anh2"].ToString();
                        obj.Anh3 = reader["Anh3"].ToString();
                        obj.Anh4 = reader["Anh4"].ToString();
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.MaKhoaCanLamSang = reader["MaKhoaCanLamSang"].ToString();
                        obj.MaPhongCanLamSang = reader["MaPhongCanLamSang"].ToString();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.MaKhoaKham = reader["MaKhoaKham"].ToString();
                        obj.MaPhongKham = reader["MaPhongKham"].ToString();
                        obj.TrangThaiCho = String.IsNullOrEmpty(reader["TrangThaiCho"].ToString()) == true ? false : (bool)reader["TrangThaiCho"];
                        obj.MaBenhNhan = reader["MaBenhNhan"].ToString();
                        obj.TenBenhNhan = reader["TenBenhNhan"].ToString();
                        obj.NgaySinh = reader["NgaySinh"].ToString();
                        obj.ThangSinh = reader["ThangSinh"].ToString();
                        obj.NamSinh = (Int32)reader["NamSinh"];
                        obj.MaGioiTinh = reader["MaGioiTinh"].ToString();
                        obj.TenGioiTinh = reader["TenGioiTinh"].ToString();
                        obj.MaDanToc = reader["MaDanToc"].ToString();
                        obj.TenDanToc = reader["TenDanToc"].ToString();
                        obj.MaNgheNghiep = reader["MaNgheNghiep"].ToString();
                        obj.TenNgheNghiep = reader["TenNgheNghiep"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuThamDoChucNang", ex.Message); throw ex; }
        }

        public List<PhieuThamDoChucNangEntity> GetPhieuThamDoChucNangList()
        {
            try
            {
                List<PhieuThamDoChucNangEntity> list = new List<PhieuThamDoChucNangEntity>();
                PhieuThamDoChucNangEntity obj = null;
                string sql = "spPhieuThamDoChucNang_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuThamDoChucNangEntity();
                        obj.MaThamDoChucNang = reader["MaThamDoChucNang"].ToString();
                        obj.Stt = String.IsNullOrEmpty(reader["Stt"].ToString()) == true ? 0 : (Int32)reader["Stt"];
                        obj.SoVaoVien = reader["SoVaoVien"].ToString();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.YeuCau = reader["YeuCau"].ToString();
                        obj.GhiChu = reader["GhiChu"].ToString();
                        obj.KetQua = reader["KetQua"].ToString();
                        obj.KetLuan = reader["KetLuan"].ToString();
                        obj.MaBacSyKham = reader["MaBacSyKham"].ToString();
                        obj.MaBacSyCls = reader["MaBacSyCls"].ToString();
                        obj.Anh1 = reader["Anh1"].ToString();
                        obj.Anh2 = reader["Anh2"].ToString();
                        obj.Anh3 = reader["Anh3"].ToString();
                        obj.Anh4 = reader["Anh4"].ToString();
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.MaKhoaCanLamSang = reader["MaKhoaCanLamSang"].ToString();
                        obj.MaPhongCanLamSang = reader["MaPhongCanLamSang"].ToString();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.MaKhoaKham = reader["MaKhoaKham"].ToString();
                        obj.MaPhongKham = reader["MaPhongKham"].ToString();
                        obj.TrangThaiCho = String.IsNullOrEmpty(reader["TrangThaiCho"].ToString()) == true ? false : (bool)reader["TrangThaiCho"];
                        obj.MaBenhNhan = reader["MaBenhNhan"].ToString();
                        obj.TenBenhNhan = reader["TenBenhNhan"].ToString();
                        obj.NgaySinh = reader["NgaySinh"].ToString();
                        obj.ThangSinh = reader["ThangSinh"].ToString();
                        obj.NamSinh = (Int32)reader["NamSinh"];
                        obj.MaGioiTinh = reader["MaGioiTinh"].ToString();
                        obj.TenGioiTinh = reader["TenGioiTinh"].ToString();
                        obj.MaDanToc = reader["MaDanToc"].ToString();
                        obj.TenDanToc = reader["TenDanToc"].ToString();
                        obj.MaNgheNghiep = reader["MaNgheNghiep"].ToString();
                        obj.TenNgheNghiep = reader["TenNgheNghiep"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuThamDoChucNangList", ex.Message); throw ex; }
        }

        public List<PhieuThamDoChucNangEntity> GetPhieuThamDoChucNangByTrangThai(Int32 trangThai)
        {
            try
            {
                List<PhieuThamDoChucNangEntity> list = new List<PhieuThamDoChucNangEntity>();
                PhieuThamDoChucNangEntity obj = null;
                string sql = "spPhieuThamDoChucNang_SelectByTrangThai";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.String, trangThai);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuThamDoChucNangEntity();
                        obj.MaThamDoChucNang = reader["MaThamDoChucNang"].ToString();
                        obj.Stt = String.IsNullOrEmpty(reader["Stt"].ToString()) == true ? 0 : (Int32)reader["Stt"];
                        obj.SoVaoVien = reader["SoVaoVien"].ToString();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.YeuCau = reader["YeuCau"].ToString();
                        obj.GhiChu = reader["GhiChu"].ToString();
                        obj.KetQua = reader["KetQua"].ToString();
                        obj.KetLuan = reader["KetLuan"].ToString();
                        obj.MaBacSyKham = reader["MaBacSyKham"].ToString();
                        obj.MaBacSyCls = reader["MaBacSyCls"].ToString();
                        obj.Anh1 = reader["Anh1"].ToString();
                        obj.Anh2 = reader["Anh2"].ToString();
                        obj.Anh3 = reader["Anh3"].ToString();
                        obj.Anh4 = reader["Anh4"].ToString();
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.MaKhoaCanLamSang = reader["MaKhoaCanLamSang"].ToString();
                        obj.MaPhongCanLamSang = reader["MaPhongCanLamSang"].ToString();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.MaKhoaKham = reader["MaKhoaKham"].ToString();
                        obj.MaPhongKham = reader["MaPhongKham"].ToString();
                        obj.TrangThaiCho = String.IsNullOrEmpty(reader["TrangThaiCho"].ToString()) == true ? false : (bool)reader["TrangThaiCho"];
                        obj.MaBenhNhan = reader["MaBenhNhan"].ToString();
                        obj.TenBenhNhan = reader["TenBenhNhan"].ToString();
                        obj.NgaySinh = reader["NgaySinh"].ToString();
                        obj.ThangSinh = reader["ThangSinh"].ToString();
                        obj.NamSinh = (Int32)reader["NamSinh"];
                        obj.MaGioiTinh = reader["MaGioiTinh"].ToString();
                        obj.TenGioiTinh = reader["TenGioiTinh"].ToString();
                        obj.MaDanToc = reader["MaDanToc"].ToString();
                        obj.TenDanToc = reader["TenDanToc"].ToString();
                        obj.MaNgheNghiep = reader["MaNgheNghiep"].ToString();
                        obj.TenNgheNghiep = reader["TenNgheNghiep"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuThamDoChucNangList", ex.Message); throw ex; }
        }

    }
}